<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/20
 * Time: 11:14
 */

namespace app\index\controller;


use app\admin\model\AssembleUser;
use app\admin\model\ClassType;
use app\admin\model\Coach;
use app\admin\model\GoodsList;
use app\admin\model\OrderList;
use app\index\model\JsApiTicket;
use app\admin\model\Tips;
use think\Db;
use think\facade\Session;

class Order extends Base
{
    public function orderList(){
        $this->assign('pageType','orderList');
        return $this->fetch();
    }
    public function orderInfo(){
        return $this->fetch();
    }
    public function orderShow(){
        return $this->fetch();
    }
    public function share(){
        $whereAFa['wx_id']=1;
        $wechatRes=Db::name('wechatconfig')->where($whereAFa)->find();
        $this->assign("wechatRes",$wechatRes);

        header("Content-Type:text/html;charset=utf-8");
        define('WX_APPID',$wechatRes['appid']);
        define('WX_MCHID',$wechatRes['mch_id']);
        define('WX_KEY',$wechatRes['paykey']);
        define('WX_APPSECRET',$wechatRes['appsecret']);

        //分享传值
        $wechat=new JsApiTicket();
        $config=$wechat->getSignPackage();

        $this->assign('appid',$config['appId']);
        $this->assign('timestamp',$config['timestamp']);
        $this->assign('noncestr',$config['nonceStr']);
        $this->assign('signature',$config['signature']);
        $url='http://127.0.0.1';
        $this->assign('url',$url);


        $this->assign('title','123');
        $this->assign('imgUrl',$url.'/uploads/20190523/dcc0fba9f13f25f1905be94494c61bce.png');
        return $this->fetch();
    }
    public function orderData(){
        $page = input('post.page');
        $limit = input('post.limit');
        $user_id = Session::get('user_id');
        $data = OrderList::where('user_id',$user_id)
            ->where('order_status',1)->page($page,$limit)
            ->order('create_time desc')->select()->each(function ($item){
                if ($item->order_type == 1 && $item->test_type == 1){
                    $item->order_text = '温馨提示';
                    $item->order_url  = '/index/order/tips';
                    $item->order_status  = '已付款';
                    $item->order_types  = '单买';
                }elseif ($item->order_type == 2 && $item->test_type == 1){
                    $f_id = $item->f_id?$item->f_id:$item->id;
                    $ass = AssembleUser::where('order_id',$f_id)->find();
                    if ($ass['status'] == 2){
                        $item->order_text = '温馨提示';
                        $item->order_url  = '/index/order/tips';
                        $item->order_status  = '已付款';
                        $item->order_types  = '拼团';
                    }elseif ($ass['status'] == 1){
                        $item->order_text = '分享好友参团';
                        $item->order_url  = '/index/assemble/assShare?id='.$ass['id'];
                        $item->order_status  = '拼团中';
                        $item->order_types  = '拼团';
                    }else{
                        $item->order_text = '拼团失败';
                        $item->order_url  = 'javascript:;';
                        $item->order_status  = '已付款';
                        $item->order_types  = '拼团';
                    }
                }elseif ($item->test_type == 2){
                    $item->order_text = '补考 '.ClassType::where('id',$item->course_type)->value('class_name');
                    $item->order_url  = 'javascript:;';
                    $item->order_status  = '已付款';
                    $item->order_types  = '单买';
                }elseif ($item->test_type == 3){
                    $item->order_text = '预约练车';
                    $item->order_url  = 'javascript:;';
                    $item->order_status  = '已付款';
                    $item->order_types  = '单买';
                }
            })->toArray();
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data]);
    }

    public function orderNotData(){
        $page = input('post.page');
        $limit = input('post.limit');
        $user_id = Session::get('user_id');
        $data = OrderList::where('user_id',$user_id)
            ->where('order_status',0)->page($page,$limit)
            ->order('create_time desc')->select()->each(function ($item){
                $item->order_status = '待付款';
                if ($item->order_type == 2){
                    $item->order_type = '拼团';
                }else{
                    $item->order_type = '单买';
                }
            })->toArray();
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data]);
    }


    public function tips(){
        $data = Tips::where('id',1)->value('content');
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function info($id){
        $order = OrderList::where('id',$id)->find();
        $coach = Coach::where('id',$order['coach_id'])->find();
        $this->assign('order',$order);
        $this->assign('coach',$coach);
        return $this->fetch();
    }
}